package com.gz.mylife.widget;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.ImageView;

import com.gz.mylife.R;
import com.gz.mylife.util.ImageUtils;
import com.zl.smartmall.library.util.Tools;

/**
 * Advert Page Indicator
 * 
 * @author 陈希然
 */
public class EmojiPageIndicator extends ImageView {
	private static final String TAG = "PageIndicatorView";
	// Current page indicator bitmap
	private Bitmap mCurrentIndicator;
	// Normal page indicator bitmap
	private Bitmap mNormalIndicator;
	// Whole indicator bitmap
	private Bitmap mIndicatorBitmap;
	// Whole indicator bitmap width
	private int mIndicatorBarWidth = 0;
	// Whole indicator bitmap width
	private int mIndicatorBarHeight = 0;

	private int mLeftMargin = 5;
	private Context mContext;

	public EmojiPageIndicator(Context context) {
		super(context);
		initView(context);
	}

	public EmojiPageIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	public EmojiPageIndicator(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView(context);
	}

	private void initView(Context context) {
		mContext = context;
		final Resources res = getResources();

		mCurrentIndicator = ImageUtils.zoomBitmapByMatrix(
				BitmapFactory.decodeResource(res,R.drawable.circle_select),
				Tools.dip2px(context, 7),
				Tools.dip2px(context, 7));
		mNormalIndicator = ImageUtils.zoomBitmapByMatrix(
				BitmapFactory.decodeResource(res,R.drawable.circle_normal),
				Tools.dip2px(context, 7),
				Tools.dip2px(context, 7));
		
		DisplayMetrics dm = new DisplayMetrics();
		WindowManager manager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		manager.getDefaultDisplay().getMetrics(dm);
		mIndicatorBarHeight = Tools.dip2px(context, 7);
		mIndicatorBarWidth = dm.widthPixels;
	}

	public void drawPageIndicator(int currentPageIndex, int screenCount) {

		// Each page indicator width
		int width;
		mIndicatorBitmap = Bitmap.createBitmap(mIndicatorBarWidth,
				mIndicatorBarHeight, Config.ARGB_8888);

		width = mNormalIndicator.getWidth();
		final Canvas canvas = new Canvas(mIndicatorBitmap);
		int left = (mIndicatorBarWidth / 2) - 
				(screenCount * mCurrentIndicator.getWidth() + ((screenCount - 1) * mLeftMargin)) / 2;
		int top = 0;

		for (int i = 0; i < screenCount; i++) {
			if (currentPageIndex == i) {
				canvas.drawBitmap(mCurrentIndicator, left, top, null);
			} else {
				canvas.drawBitmap(mNormalIndicator, left, top, null);
			}

			left = left + width + mLeftMargin;
		}

		setImageBitmap(mIndicatorBitmap);
	}

	public void clearStaticData() {
		if (mCurrentIndicator != null) {
			mCurrentIndicator.recycle();
			mCurrentIndicator = null;
		}

		if (mNormalIndicator != null) {
			mNormalIndicator.recycle();
			mNormalIndicator = null;
		}

	}

}
